package com.china.school;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.time.LocalDateTime;


//@EnableTransactionManagement
public class TransactionManagerTest {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("XmlTransactionTest.xml");

        TransactionManagerTest bean = ctx.getBean(TransactionManagerTest.class);
        bean.update();

    }

     private JdbcTemplate jdbcTemplate;

    public void update() throws Exception {
        int update = jdbcTemplate.update("update user set nick_name = '" + LocalDateTime.now().toString() + "' where user_id = 1");
        System.out.println("更新数据条数" + update);
        if (true){
            throw new Exception("故意抛出异常");
        }

    }
    public void add() throws Exception {
          jdbcTemplate.execute(String.format("insert user(user_id,nick_name,age,update_time) values(%i,%s,%i,%s)",
                null,"小明",22,LocalDateTime.now().toString().replace("T"," ")));
        System.out.println("保存一条数据"  );
//        if (true){
//            throw new Exception("故意抛出异常");
//        }

    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
